package com.kun.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.entity.Blog;
import com.kun.entity.BlogType;
import com.kun.entity.Comment;
import com.kun.result.Res;
import com.kun.result.Result;
import com.kun.service.impl.BlogServiceImpl;
import com.kun.service.impl.BlogTypeServiceImpl;
import com.kun.service.impl.CommentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * @descriptions: 后台博主登录
 * @author: SunKun
 * @date: 2021/7/5 0005 17:56
 * @version: 1.0
 */
@RestController
@CrossOrigin  //允许跨域
@ResponseBody
@RequestMapping("/kun/admin/index")
public class IndexAdminController {
    @Autowired
    private BlogServiceImpl blogService;
    @Autowired
    private CommentServiceImpl commentService;
    @Autowired
    private BlogTypeServiceImpl blogTypeService;

    /**
     * 默认分页查询
     * @return
     */
    @PostMapping("/queryPage")
    public Res queryPage(int page, int limit){
        Res res = new Res();
        //创建分页对象，指定当前页码及每页显示数量
        Page<Blog> blogPage = new Page<>(page,limit);
        //分页查询
        IPage<Blog> blogIPage = blogService.queryPage(blogPage);
        res.setCount(blogIPage.getTotal());
        res.setData(blogIPage.getRecords());
        return res;
    }

    /**
     * 删除博客
     * @param param
     * @return
     * 删除博客 还需要删除评论
     */
    @PostMapping("/del")
    public Result delBlog(@RequestBody Map<String,Object> param){
        int id = (int)param.get("id");
        commentService.delComment(id);//删除评论id
        return blogService.delBlog(id) != 0 ? Result.ok() : Result.error();
    }

    /**
     * 评论分页
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/commentPage")
    public Res commentPage(int page, int limit){
        Res res = new Res();
        Page<Comment> blogPage = new Page<>(page,limit);
        IPage<Comment> commentIPage = commentService.commentPage(blogPage);
        res.setCount(commentIPage.getTotal());
        res.setData(commentIPage.getRecords());
        return res;
    }

    /**
     * 后台博客类型查询分页
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/blogTypePage")
    public Res blogTypePage(int page,int limit){
        Res res = new Res();
        Page<BlogType> blogPage = new Page<>(page,limit);
        IPage<BlogType> blogIPage = blogTypeService.blogTypePage(blogPage);
        res.setCount(blogIPage.getTotal());
        res.setData(blogIPage.getRecords());
        return res;
    }
}
